package com.eigaland.change_machine.business.show

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.AppCompatButton
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eigaland.change_machine.BR
import com.eigaland.change_machine.R
import com.eigaland.change_machine.adapter.BaseBindAdapter
import com.eigaland.change_machine.adapter.ViewPagerViewAdapter
import com.eigaland.change_machine.business.movielist.MovieListItemVM
import com.eigaland.change_machine.business.selectseat.SelectSeatFragment
import com.eigaland.change_machine.business.selectseat.SelectSeatFragmentRepository
import com.eigaland.change_machine.displaymodel.ShowDisplayModel
import com.eigaland.change_machine.util.ProgressDialogManager
import com.eigland.eigaland_lib.base.BaseFragment
import com.eigland.eigaland_lib.base.BaseLazyFragment
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import com.yarolegovich.discretescrollview.transform.Pivot
import com.yarolegovich.discretescrollview.transform.ScaleTransformer
import kotlinx.android.synthetic.main.fragment_movie_list_full_screen.*
import kotlinx.android.synthetic.main.fragment_show_list.*
import kotlinx.android.synthetic.main.include_just_recyeleview.*
import kotlinx.android.synthetic.main.include_just_recyeleview.view.*
import javax.xml.transform.Transformer

public class ShowListFragment :
    BaseLazyFragment<ShowListFragmentVM, com.eigaland.change_machine.databinding.FragmentShowListBinding>() {

    var curPosition = 0

    override fun initVM() {

        var movieId =   arguments?.getString("movieId")
        // init data
        vm?.initData()
        vm?.movieListDataVMS?.observe(this, Observer {
            var adapter = BaseBindAdapter<MovieListItemVM>(R.layout.item_movie_list_data_full_screen, BR.movieListItemVM)
            adapter.addData(it.movieItems!!)
            var t = ScaleTransformer.Builder()
            t.setMaxScale(1.0f)
            t.setMinScale(0.8f)
            t.setPivotX(Pivot.X.CENTER)
            t.setPivotY(Pivot.Y.BOTTOM)
            rv_select_movie.setItemTransformer(t.build())
            rv_select_movie.adapter = adapter
            rv_select_movie.addOnItemChangedListener { viewHolder, position ->

                    movieId = it?.movieItems!![position].movieId
                    curPosition = position
                    vm?.curMovieName?.set(it?.movieItems!![position].movieName)
                    vm?.loadShowListWith(it?.movieItems!![position].movieId, vm?.dateListVM?.value?.get(0)!!.apiDate)
            }

            it.movieItems?.forEachIndexed { index, movieListItemVM ->
                if(movieListItemVM.movieId == movieId) {
                    vm?.curMovieName?.set(it?.movieItems!![index].movieName)
                    view?.postDelayed({
                        rv_select_movie.scrollToPosition(index)
                    },300)

                }
            }
            vm?.loadDateList()
        })

        vm?.progressLoadingStatus?.observe(this, Observer {
            if(it) ProgressDialogManager.showDialog(requireContext())
            else ProgressDialogManager.dismissDialog(requireContext())
        })

        /**
         *  show data call back success
         */
        vm?.showListVM?.observe(this, Observer {
            var list = ArrayList<View>()

            if(it.isEmpty()) {
                var view = LayoutInflater.from(requireContext()).inflate(R.layout.include_just_recyeleview,null)
                var adapter = BaseBindAdapter<ShowListItemVM>(R.layout.item_show_list, BR.showListItemVM);
                view.recyclerview.layoutManager = LinearLayoutManager(requireContext())
                view.recyclerview.adapter =  adapter
                list.add(view)
            }

            for (index in 0 until it.size) {
                var view = LayoutInflater.from(requireContext()).inflate(R.layout.include_just_recyeleview,null)
                view.recyclerview.layoutManager = LinearLayoutManager(requireContext())
                var adapter = BaseBindAdapter<ShowListItemVM>(R.layout.item_show_list, BR.showListItemVM);

                it[index].forEach {inside ->
                    inside.clickStatus.observe(this, Observer {
                        var selectSeatFragment = SelectSeatFragment()
                        var bundle = Bundle()



                        bundle.putSerializable("show",it)
                        bundle.putSerializable("movie", vm?.movieListDataVMS?.value?.movieItems?.get(curPosition))
                        selectSeatFragment.arguments = bundle
                        supportDelegate.start(selectSeatFragment)
                    })
                }
                adapter.setNewData(it[index])
                view.recyclerview.adapter =  adapter
                list.add(view)
            }

            show_indicator.setData(list.size,0)
            show_indicator.setUpViewPager(show_list_viewpager)
            show_list_viewpager.adapter = ViewPagerViewAdapter(list)
        })

        /**
         * date list call back
         */
        vm?.dateListVM?.observe(this, Observer {

            var adapter = BaseBindAdapter<ShowListDateVM>(R.layout.item_show_list_date, BR.showListDateVM)
            adapter.setNewData(it)
            adapter.setOnItemClickListener { adapter, view, position ->
               it.forEachIndexed { index, showListDateVM ->
                   showListDateVM.isCheckCurrent = index == position
               }
               adapter.setNewData(it)
               adapter.notifyDataSetChanged()

               vm?.loadShowListWith(movieId!!,it[position].apiDate)

            }
            rv_select_date.layoutManager = LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
            rv_select_date.adapter = adapter

        })

    }

    override fun setLayoutId() = R.layout.fragment_show_list

    override fun setUpViewData(view: View) {
        vm = ShowListFragmentVM()
        binding = DataBindingUtil.bind(view)
        binding?.let {
            it.showListVM = vm
            it.lifecycleOwner = this
        }
    }
}